#include "pagepower.h"

phasorDiagram::phasorDiagram(QWidget *parent)
    : QWidget(parent)
{
    phase[0] = 0;
    phase[1] = 120;
    phase[2] = 240;

    phase[3] = 10;
    phase[4] = 130;
    phase[5] = 250;
}

void phasorDiagram::drawCustomLine(QPainter& _painter, double _angle, double _len, const QString& _name, const QColor& _color)
{
    // 初始化
    _painter.resetTransform(); // 重设画笔
    _painter.translate(QPoint(150,150));
    _painter.setPen(QPen(_color, 2));

    // 绘制线段
    QLine _line(QPoint(0,0), QPoint(_len,0));
    _painter.rotate(_angle);
    _painter.drawLine(_line);

    // 绘制箭头
    int off_x = 10;
    QPainterPath _path;
    _path.moveTo(_len-off_x, -5);
    _path.lineTo(_len,0);
    _path.lineTo(_len-off_x,5);
    _painter.drawPath(_path);

    // 绘制名称
    _painter.setPen(_color);

    QFont _font = _painter.font();
    _font.setPointSize(15);
    _font.setBold(true);
    _painter.setFont(_font);

    _painter.drawText(QPoint(_len+off_x,10), _name);
}

void phasorDiagram::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::gray, 2));
    painter.setRenderHint(QPainter::Antialiasing);

    //painter.drawRect(20,20,260,260);
    painter.drawEllipse(50,50,200,200);

    drawCustomLine(painter, phase[0], 100, "Ua", QColor(238,238,0));
    drawCustomLine(painter, phase[1], 100, "Ub", Qt::green);
    drawCustomLine(painter, phase[2], 100, "Uc", Qt::red);

    drawCustomLine(painter, phase[3], 100, "Ia", QColor(205,149,12));
    drawCustomLine(painter, phase[4], 100, "Ib", Qt::darkGreen);
    drawCustomLine(painter, phase[5], 100, "Ic", Qt::darkRed);
}

powerPage::powerPage(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(1000,300);

    paintArea = new phasorDiagram(this);
    paintArea->setFixedSize(300,300);

    setStyleSheet(".QLabel{color:gray; font-size:14px;border:1px solid gray;min-height:40px;}");

    QString sty_data="color: white; font-weight:bold;";

    labRMS[0] = new QLabel("0.0000");
    labRMS[0]->setStyleSheet(sty_data);
    labRMS[1] = new QLabel("0.0000");
    labRMS[1]->setStyleSheet(sty_data);
    labRMS[2] = new QLabel("0.0000");
    labRMS[2]->setStyleSheet(sty_data);

    labRMS[3] = new QLabel("0.0000");
    labRMS[3]->setStyleSheet(sty_data);
    labRMS[4] = new QLabel("0.0000");
    labRMS[4]->setStyleSheet(sty_data);
    labRMS[5] = new QLabel("0.0000");
    labRMS[5]->setStyleSheet(sty_data);

    labPowerA = new QLabel("0.0000");
    labPowerA->setStyleSheet(sty_data);
    labPowerB = new QLabel("0.0000");
    labPowerB->setStyleSheet(sty_data);
    labPowerC = new QLabel("0.0000");
    labPowerC->setStyleSheet(sty_data);

    labPowerFactorA = new QLabel("0.0000");
    labPowerFactorA->setStyleSheet(sty_data);
    labPowerFactorB = new QLabel("0.0000");
    labPowerFactorB->setStyleSheet(sty_data);
    labPowerFactorC = new QLabel("0.0000");
    labPowerFactorC->setStyleSheet(sty_data);

    labPowerReactiveA = new QLabel("0.0000");
    labPowerReactiveA->setStyleSheet(sty_data);
    labPowerReactiveB = new QLabel("0.0000");
    labPowerReactiveB->setStyleSheet(sty_data);
    labPowerReactiveC = new QLabel("0.0000");
    labPowerReactiveC->setStyleSheet(sty_data);

    QGridLayout *layoutRight = new QGridLayout;
    layoutRight->setContentsMargins(0,50,0,0);
    layoutRight->setSpacing(0);

    layoutRight->addWidget((new QLabel("电压(kV)")), 0, 1, 1, 1);
    layoutRight->addWidget((new QLabel("电流(A)")), 0, 2, 1, 1);
    layoutRight->addWidget((new QLabel("功率因数")), 0, 3, 1, 1);
    layoutRight->addWidget((new QLabel("有功功率(kW)")), 0, 4, 1, 1);
    layoutRight->addWidget((new QLabel("无功功率(kVar)")), 0, 5, 1, 1);

    layoutRight->addWidget((new QLabel("A相")), 1, 0, 1, 1);
    layoutRight->addWidget((new QLabel("B相")), 2, 0, 1, 1);
    layoutRight->addWidget((new QLabel("C相")), 3, 0, 1, 1);

    layoutRight->addWidget(labRMS[0], 1, 1, 1, 1);
    layoutRight->addWidget(labRMS[1], 2, 1, 1, 1);
    layoutRight->addWidget(labRMS[2], 3, 1, 1, 1);

    layoutRight->addWidget(labRMS[3], 1, 2, 1, 1);
    layoutRight->addWidget(labRMS[4], 2, 2, 1, 1);
    layoutRight->addWidget(labRMS[5], 3, 2, 1, 1);

    layoutRight->addWidget(labPowerFactorA, 1, 3, 1, 1);
    layoutRight->addWidget(labPowerFactorB, 2, 3, 1, 1);
    layoutRight->addWidget(labPowerFactorC, 3, 3, 1, 1);

    layoutRight->addWidget(labPowerA, 1, 4, 1, 1);
    layoutRight->addWidget(labPowerB, 2, 4, 1, 1);
    layoutRight->addWidget(labPowerC, 3, 4, 1, 1);

    layoutRight->addWidget(labPowerReactiveA, 1, 5, 1, 1);
    layoutRight->addWidget(labPowerReactiveB, 2, 5, 1, 1);
    layoutRight->addWidget(labPowerReactiveC, 3, 5, 1, 1);

    layoutRight->setRowStretch(6, 1);

    QHBoxLayout *layoutMain = new QHBoxLayout;
    layoutMain->addWidget(paintArea);
    layoutMain->addLayout(layoutRight);

    setLayout(layoutMain);
}
